package thirdPartyUtils.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.junit.Test;

import java.util.*;

public class Excel {
    private ExcelWriter excelWriter;

    public Excel generateExcel(String outName, String templateName){
        this.excelWriter = EasyExcel.write(outName).withTemplate(templateName).build();
        return this;
    }

    public Excel addSheet(Object data, String SheetName){
        WriteSheet sheet = EasyExcel.writerSheet(SheetName).build();
        excelWriter.fill(data, sheet);
        return this;
    }

    public void finish(){
        excelWriter.finish();
    }


    @Test
    public void writeAll(String[] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date", "2019年10月9日13:28:28");
        map.put("total", 1000);

        EasyExcel.write("complexFillOut.xlsx").withTemplate("complexFill.xlsx").build()
                 .fill(map, EasyExcel.writerSheet("Sheet1").build())
                 .finish();
    }

    public List<User> data(){
        List<User> userList = new ArrayList<User>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setUsername("用户" + i);
            user.setPassword(String.valueOf(Math.random()));
            user.setBirthday(new Date());
            user.setMoney(3.14);
            userList.add(user);
        }
        return userList;
    }
}
